package com.zzy.demo.web;

import java.nio.charset.Charset;
import java.util.Base64;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zzy.demo.common.util.encode.AesEncryptUtil;
import com.zzy.demo.common.util.encode.DesEncryptUtil;
import com.zzy.demo.common.util.encode.HexUtil;

@Controller
public class CrytoController {

	/**
	 * md5   16进制加密dex   base64
	 * @return
	 */
	@RequestMapping("/crytojs")
	public String crytojs() {
		return "cryptojs";
	}
	

	@RequestMapping("/crytoDes")
	public String crytoDes() {
		return "crypto_des";
	}
	
	@RequestMapping("/crytoDes/decode")
	@ResponseBody
	public String crytoDesDecode(HttpServletRequest request, HttpServletResponse response, Model model) {
		String keyword = request.getParameter("keyword");
		String key="1234567812345678";
		String result=DesEncryptUtil.decrypt(keyword, key);
		System.out.println(result);
		return result;
	}
	
	
	
	@RequestMapping("/crytoAes")
	public String crytoAes() {
		return "crypto_aes";
	}

	@RequestMapping("/crytoAes/decode")
	@ResponseBody
	public String crytoAesDecode(HttpServletRequest request, HttpServletResponse response, Model model) {
		String keyword = request.getParameter("keyword");
		String key="0102030405060708";
		String iv="0102030405060708";
		String result=AesEncryptUtil.decrypt(keyword, key, iv);
		System.out.println(result);
		return result;
	}
	
	
	
	@RequestMapping("/crytoHex")
	public String crytoHex() {
		return "crypto_hex";
	}
	
	
	@RequestMapping("/crytoHex/decode")
	@ResponseBody
	public String crytoHexDecode(HttpServletRequest request) {
		String keyword = request.getParameter("keyword");
		String result=HexUtil.decodeHexString(keyword);
		System.out.println(result);
		return result;
	}
	
	
	
	@RequestMapping("/crytoBase64")
	public String crytoBase64() {
		return "crypto_base64";
	}
	
	
	@RequestMapping("/crytoBase64/decode")
	@ResponseBody
	public String crytoBase64Decode(HttpServletRequest request) {
		String keyword = request.getParameter("keyword");
		byte[] resultByte = Base64.getDecoder().decode(keyword.getBytes(Charset.forName("UTF-8")));
		String result = new String(resultByte, Charset.forName("UTF-8"));
		System.out.println(result);
		return result;
	}

}
